import { Util } from "../../utils/util";
import { InventoryInOutType } from './inventory.js';

/// 出入库类型
export class InventoryType {
    constructor({
        id = '',
        name = '',
        code = '',
        inOutType = InventoryInOutType.Uknown,
        isPlatfromType = false
    }) {
        this.id = id;
        this.name = name;
        this.code = code;
        this.inOutType = inOutType;
        this.isPlatfromType = isPlatfromType;

        if(Util.isEmpty(this.code)) {
            // 如果没有code，认为是新建的类型
            this.code = Date.now().toString();
        }
    }

    /// 创建空实例
    static empty() {
        const instance = new InventoryType({});
        // empty 人为是新建，非系统类型取时间戳为code
        instance.code = Date.now().toString();
        return instance;
    }

    /// 克隆实例
    static clone(type) {
        return new InventoryType({
            id: type.id,
            name: type.name,
            code: type.code,
            inOutType: type.inOutType,
            isPlatfromType: type.isPlatfromType
        });
    }



    /// 从JSON创建实例
    static fromJson(json) {
        const id = Util.jsonToString(json['id']);
        const name = Util.jsonToString(json['typeName']);
        const code = Util.jsonToString(json['typeCode']);

        const inOutType = InventoryInOutType.parseInOutType(json['typeSource'])


        const instance = new InventoryType({
            id,
            name,
            code,
            inOutType,
            isPlatfromType: false
        });

        // 平台类型判断
        if (code.length > 0 && code.charCodeAt(0) >= 65) { // A ASCII码
            instance.isPlatfromType = true;
        }

        return instance;
    }

    /// 
    toCopyJson() {
        return {
            id: this.id,
            typeName: this.name,
            typeCode: this.code,
            typeSource: this.inOutType,
            isPlatfromType: this.isPlatfromType
        };
    }
}